﻿
<%@ Page %>
<html>
<head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312"/>
<title>Add Fields</title>
<link rel="stylesheet" type="text/css" href="/_common/styles/global.css">
<link rel="stylesheet" type="text/css" href="/_common/styles/select.css">
<link rel="stylesheet" type="text/css" href="/_nav/tabs.css">
<link rel="stylesheet" type="text/css" href="/Tools/PreviewManager/Styles/dialogs.css">
<style>

	TD.gridspacer
	{
		width:				2px;
	}
	TD.gridheader
	{
		padding-left:		8px;
	}
	TABLE.gridheader
	{
		background-color:	#e9e7d7;
		background-image:	url(/_grid/imgs/bar_bk.gif);
		font-size:			11px;
		width:				100%;
		height:				22px;
		border:				1px solid #dbdac9;
		border-bottom:		1px solid #c5c1b1;
	}

</style>
<script language="javascript" src="/_controls/settings.aspx"></script>
<script language="javascript" src="/_common/scripts/global.js"></script>
<script language="javascript" src="/Tools/PreviewManager/Scripts/objects.js"></script>
<script language="javascript" src="/Tools/PreviewManager/Scripts/dialogs.js"></script>
<script language="javascript" src="/Tools/PreviewManager/Scripts/fielddialogs.js"></script>
<script language="JavaScript">

	var	_oLastGlow;
	var _oFieldsXml;
	var _oPropertiesXml;
	var _oPreviewXml;
	var _spaces;
	
	var _oXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
	_oXmlDoc.async = false;

	var _oXslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
	_oXslDoc.async = false;

	var _oXslt = new ActiveXObject("Msxml2.XSLTemplate");
	
	var _oXslProc;


	function window.onload()
	{
		
		_oFieldsXml		= window.dialogArguments.FieldsXml;
		_oPropertiesXml	= window.dialogArguments.FieldPropertiesXml;
		_oPreviewXml	= window.dialogArguments.PreviewXml;
		_spaces			= window.dialogArguments.Spaces;
		
		// build the list of available fields
		//
		_oXslDoc.load("/Tools/FormEditor/Dialogs/fields.xsl"); //using the file from the form editor
		
		_oXslt.stylesheet = _oXslDoc;
		
		_oXmlDoc.loadXML(GetFieldsXml());
		
		_oXslProc = _oXslt.createProcessor();
		_oXslProc.input = _oXmlDoc;

		_oXslProc.transform();
		
		FieldList.innerHTML = _oXslProc.output;
		
	}


	function Ok()
	{
		var oFields = new Array();
		var oDisplayNames;

		var i = 0;
		var ii = 0;
		var iLen = Fields.rows.length;
		var total=0;
		
		
		for (i=0; i < iLen; i++)
		{
			if (Fields.rows[i].cells[0].firstChild.checked)
			{
				total ++;
			}
		}
		
		
		if (total > _spaces )
		{						
			if (0 == _spaces)
			{
				alert ( "You cannot add any more fields to this preview because the maximum number of fields has been reached." );
			}
			else
			{
				alert ( "Too many fields are selected.  Clear some check boxes and try again.");
			}
		}
		else
		{
			for (i=0; i < iLen; i++) 
			{
				if (Fields.rows[i].cells[0].firstChild.checked)
				{
					
					oDisplayNames = new Array(new LocalizedObj(Fields.rows[i].cells[1].innerText, ORG_LANGUAGE_CODE));
					
					oFields[ii]	= new FieldObj(
									Fields.rows[i].cells[2].innerText,
									Fields.rows[i].cells[3].innerText,
									(Fields.rows[i].required == "true"),
									Fields.rows[i].orgrequired,
									oDisplayNames,
									oDisplayNames,
									null,
									null
								);
					ii++;
				}
			}
			window.returnValue = (!oFields.length) ? false : oFields;
			window.close();
		}
	}


	// returns a sting of xml containing all of the available fields
	//
	function GetFieldsXml()
	{			
		var sFieldName;			
		var sFilter = BuildUsedFieldFilter(_oPreviewXml.selectNodes("/preview/rows/row/cell"));
		
		// get all the field nodes from the field properties xml minus the fields already in use by the preview
		var oFields = _oPropertiesXml.selectNodes("/entity/fields/field" + ((sFilter.length > 0) ? "[" + sFilter + "]" : ""));
		
		// add each unused field to the fields xml which will later be passed to the add fields dialog
		var sFieldsXml = "<fields>";
		
		var i;
		var iLen = oFields.length;
		
		for (i = 0; i < iLen; i++)
		{
			sFieldName = oFields[i].getAttribute("name");
			sFieldsXml += "<field name='" + sFieldName + "' displayname='" + Encode(oFields[i].selectSingleNode("displaynames/displayname[@languagecode = " + ORG_LANGUAGE_CODE + "]/@description").text) + "' datatype='" + _oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@datatype").text + "' required='" + _oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredforplatform").text + "' orgrequired='" + _oPropertiesXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredlevel").text + "'/>";
		}
		
		sFieldsXml += "</fields>";
		
		return sFieldsXml;
	}
	
	function Encode(s)
	{
		return s.replace(/\x27/g, "&#x27;");
	}
	
	function BuildUsedFieldFilter(oNodes)
	{
		var sFilter = "";

		var i;
		var iLen = oNodes.length;

		for (i = 0; i < iLen; i++)
		{
			if (i == 0)
			{
				sFilter += "@name != '" + oNodes[i].getAttribute("name") + "'";
			}
			else
			{
				sFilter += " and @name != '" + oNodes[i].getAttribute("name") + "'";
			}	
		}

		return sFilter;
	}
	
	
	function SelectAll()
	{
		var o = event.srcElement;
		
		var bIsChecked = o.checked;
		
		var i;
		var iLen = Fields.rows.length;
		
		for (i = 0; i < iLen; i++)
		{
			Fields.rows[i].cells[0].children[0].checked = bIsChecked;
		}
	}


	function on()
	{
		_oLastGlow = event.srcElement;

		while (_oLastGlow.tagName != "TR")
		{
			_oLastGlow = _oLastGlow.parentElement;
		}

		_oLastGlow.runtimeStyle.backgroundColor = "#ADC3E7";
	}


	function off()
	{
		if (_oLastGlow) _oLastGlow.runtimeStyle.backgroundColor = "";
	}
	
</script>
</head>
<body>
	<form name="crmDialog">
		<table style="width:100%; height:100%;" cellspacing="0" cellpadding="8">
			<tr>
				<td class="header">
					<div class="header">Add Fields</div>
					<div class="headerdesc">Select the fields you want to add to the preview.</div>
				</td>
			</tr>
			<tr>
				<td class="main">
					<table width="100%" cellspacing="0" cellpadding="0">
						<tr height="22">
							<td>
								<table cellpadding="0" cellspacing="0" class="gridheader">
									<tr>
										<td width="24" style="padding-left: 4px;"><input type="checkbox" class="checkbox" onclick="SelectAll();"></td>
										<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
										<td class="gridheader">Field Name</td>
										<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
										<td class="gridheader" width="168">Schema Name</td>
										<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
										<td class="gridheader" width="64">Type</td>
										<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
										<td width="14">&nbsp;</td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td>
								<div id="FieldList" style="background-color: #ffffff; height: 250px; border-left: 1px solid #cccccc; overflow-y: scroll; overflow-x: hidden;">
								</div>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td class="buttons">
					<button id="btnDialogOk" onclick="Ok();">OK</button>
					&nbsp;
					<button id="btnDialogCancel" onclick="Cancel();">Cancel</button>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
